﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SRAGWMCLib.OO;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.Collections.Generic;

namespace SRAGWMap
{
    public static class FeatureLoader
    {
        /// <summary>
        /// Contains all the features and the data to fill them
        /// </summary>
        /// <param name="xmlFilePath">Path of the file with the dat to fill the features</param>
        /// <param name="Pnts">Coordinate points that are defined previusly</param>
        /// <returns></returns>
        public static LegendEntryCollection Load(object[] Pnts)
        {
            string Tooltip;
            int NumPairs;

            NumPairs = (Pnts.Length / 2);

            IList<string> attributes = new List<string>();
            LegendEntryCollection coll = new LegendEntryCollection();
            {
                try
                {
                    #region  ------tem ligacao a base de dados GFeatures e definicao de atributos------
                    //create the database connection
                    OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\DBGEO\\Madeira_light.mdb");
                    OleDbCommand aCommand = new OleDbCommand(" SELECT FeatureName, PrimaryGeometryFieldName From GFeatures ", aConnection);
                    try
                    {
                        aConnection.Open();
                        //create the datareader object to connect to table
                        OleDbDataReader aReader = aCommand.ExecuteReader();
                        Console.WriteLine("This is the returned data from emp_test table");
                        //Iterate throuth the database
                        int colorNumber = 1;
                        while (/*aReader.Read() &&*/ (aReader.Read()))
                        {
                            attributes.Clear();
                            //Attributes = new string[] {};
                            OleDbCommand fieldsCommand = new OleDbCommand(
                                " SELECT FieldName From FieldLookup WHERE FeatureName = '" + aReader["FeatureName"] +
                                "' AND FieldName NOT LIKE '" + aReader["PrimaryGeometryFieldName"] + "%'",
                                aConnection);
                            try
                            {
                                //create the datareader object to connect to table
                                OleDbDataReader readerFields = fieldsCommand.ExecuteReader();

                                while (readerFields.Read())
                                {
                                    attributes.Add(readerFields["FieldName"].ToString());
                                }
                                readerFields.Close();
                            }
                            catch (Exception e)
                            {
                                string ex = e.ToString();
                            }
                            bool mapavisible = false;
                            //optar por freguesias como visivel na criacao do mapa pela primeira vez
                            if ((aReader["FeatureName"].ToString()) == "Freguesias")
                            {
                                mapavisible = true;
                            }
                            if (attributes.Count == 0)
                                continue;
                            string[] arr = attributes.ToArray();
                            Tooltip = "<%." + arr[0] + "%" + ">";
                            coll.Add(LegendEntry.LegendEntryBuilder(
                                attributes.ToArray(), 
                                Tooltip, 
                                0, //Symbology Style (ex. Tipo de linha continua, interrompida)
                                1, //Symbology Weight (ex. espessura da linha)
                                RandomColor.CreateRandomColor(colorNumber), 
                                Color.White, 
                                "CartBase", 
                                aReader["FeatureName"].ToString(), 
                                Pnts, NumPairs, 4, " " + aReader["FeatureName"].ToString(), mapavisible));
                            colorNumber++;
                            if(colorNumber==11) colorNumber = 1;
                        }
                        //close the reader
                        aReader.Close();
                        //close the connection Its important.
                        aConnection.Close();
                    }
                    //Some usual exception handling
                    catch (OleDbException e)
                    {
                        Console.WriteLine("Error: {0}", e.Errors[0].Message);
                    }
                    #endregion
                }
                //Some usual exception handling
                catch (OleDbException e)
                {
                    Console.WriteLine("Error: {0}", e.Errors[0].Message);
                }
                return coll;
            }
        }
    }
}